package cn.exrick.xboot.config.sms;


import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class AliyunSmsUtils {
    /*@Value("${aliyun.sms.accessKeyId}")
    private String  accessKeyId;

    @Value("${aliyun.sms.signName}")
    private String  signName;

    @Value("${aliyun.sms.template_code}")
    private String  template_code;
    @Value("${aliyun.sms.product}")
    private String  product;
    @Value("${aliyun.sms.accessKeySecret}")
    private String  accessKeySecret;
    @Value("${aliyun.sms.domain}")
    private String  domain;
    @Value("${aliyun.sms.regionId}")
    private String  regionId;
    @Autowired
    private SmsConfigService smsConfigService;
    public  CommonResponse sendSms(String mobile,int code) throws ClientException {
       *//* Pageable pageable = PageRequest.of(0,1, Sort.Direction.DESC,"smsConfigId");
        Page<SmsConfig> all = smsConfigService.findAll(pageable);
        if(all.getContent().size()>0){
            accessKeyId=all.getContent().get(0).getAccessKeyId();
            signName=all.getContent().get(0).getSignName();
            template_code=all.getContent().get(0).getTemplate_code();
            accessKeySecret=all.getContent().get(0).getAccessKeySecret();
        }*//*
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(domain);
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", regionId);
        request.putQueryParameter("PhoneNumbers", mobile);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", template_code);
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
        Map<String,Object> map=new HashMap<String,Object>();
        CommonResponse data=null;
        try {
            data = client.getCommonResponse(request);
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return data;
    }
    public Map<String,Object> getSmsInformation(){
        Pageable pageable = PageRequest.of(0,1, Sort.Direction.DESC,"smsConfigId");
        Page<SmsConfig> all = smsConfigService.findAll(pageable);
        if(all.getContent().size()>0){
            accessKeyId=all.getContent().get(0).getAccessKeyId();
            signName=all.getContent().get(0).getSignName();
            template_code=all.getContent().get(0).getTemplate_code();
            accessKeySecret=all.getContent().get(0).getAccessKeySecret();
        }
        Map<String,Object> map=new HashMap<String,Object>();
        //服务提供商
        map.put("serviceProvider","阿里云");
        map.put("signName",signName);
        map.put("accessKeyId",accessKeyId);
        map.put("accessKeySecret",accessKeySecret);
        map.put("template_code",template_code);
        map.put("domain",domain);
        map.put("regionId",regionId);
        return map;
    }*/

}
